package com.lh_travel.lohas.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.lh_travel.lohas.R;
import com.lh_travel.lohas.adapter.fragment_pageradpter;
import com.lh_travel.lohas.animation.ZoomOutPageTransformer;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.DafultMessageEvent;
import com.lh_travel.lohas.fragment.HomeMyFragment;
import com.lh_travel.lohas.fragment.HomeSearchFragment;
import com.lh_travel.lohas.interfaces.ContentChange;
import com.lh_travel.lohas.utils.Preferences;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

/**
 * Created by aaron on 2018/6/1.
 */

public class HomeActivity extends baseActivity implements ContentChange {

    private TabLayout tabLayout;
    private ViewPager vp_content;
    private HomeSearchFragment fragment1;
    private fragment_pageradpter adpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);

        setContentView(R.layout.activity_home);

        findView();
        bindListner();
        ensureUI();
    }

    @Override
    protected void onResume() {
        super.onResume();

        autoLocation(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {

                int errorCode = aMapLocation.getErrorCode();

                if (errorCode == 0) {

                    //定位成功
                    double latitude = aMapLocation.getLatitude();
                    double longitude = aMapLocation.getLongitude();

                    //把结果保存起来
                    mApp.setPreference(Preferences.LOCAL.LAT, latitude + "");
                    mApp.setPreference(Preferences.LOCAL.LNG, longitude + "");

                }
            }
        });
    }

    @Override
    public void findView() {
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        vp_content = (ViewPager) findViewById(R.id.vp_content);

    }

    @Override
    public void bindListner() {

    }

    @Override
    public void ensureUI() {

        ArrayList<Fragment> mList = new ArrayList<>();

        fragment1 = new HomeSearchFragment(this);
        HomeMyFragment fragment2 = new HomeMyFragment(this);

        mList.add(fragment1);
        mList.add(fragment2);

        adpter = new fragment_pageradpter(getSupportFragmentManager(), mList, null);

        vp_content.setPageTransformer(false, new ZoomOutPageTransformer());
        vp_content.setAdapter(adpter);

        tabLayout.addTab(tabLayout.newTab().setCustomView(tab_icon(mContext.getString(R.string.hotel), R.drawable.hotel)));
        tabLayout.addTab(tabLayout.newTab().setCustomView(tab_icon(mContext.getString(R.string.my_count), R.drawable.my)));

        //Tablayout自定义view绑定ViewPager
        vp_content.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(vp_content));

    }

    private View tab_icon(String name, int iconID) {
        View newtab = LayoutInflater.from(this).inflate(R.layout.item_home_tab, null);
        TextView tv = (TextView) newtab.findViewById(R.id.tabtext);
        tv.setText(name);
        ImageView im = (ImageView) newtab.findViewById(R.id.tabicon);
        im.setImageResource(iconID);
        return newtab;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleMessageEvent(DafultMessageEvent message) {
        switch (message.TAG) {

            case "ContentSearchActivity":

                fragment1.setKeyword(message.message, message.belong);

                break;

            case "ContentSearchActivity2":

                fragment1.setKeyword("我的附近", message.belong);

                break;

            case "CalendarActivity":

                fragment1.setDate(message.checkin, message.checkout);

                break;

            case "LoginActivity":

                adpter.notifyDataSetChanged();

                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister(this);
    }

    @Override
    public void change() {
        adpter.notifyDataSetChanged();
    }

    @Override
    public void onBackPressed() {
       // super.onBackPressed();

        if (toastView == null){
            showMessage("再按一次退出APP");

        }else {

            System.exit(0);

        }
    }
}
